package com.ycfmedical.bed.service;

import com.tencent.TIMCallBack;
import com.tencent.TIMManager;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.bean.ImUser;
import com.ycfmedical.bed.business.LoginBusiness;
import com.ycfmedical.bed.event.MessageEvent;
import com.ycfmedical.bed.utils.PushUtil;
import com.ycfmedical.bed.utils.ResourceUtil;

import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.log.XLog;
import tencent.tls.platform.TLSErrInfo;
import tencent.tls.platform.TLSPwdLoginListener;
import tencent.tls.platform.TLSUserInfo;

/**
 * Created by Egoist on 2017-04-05.
 */

public class LoginImService implements TIMCallBack {

    private static String TAG = LoginImService.class.getSimpleName();

    // 静态内部类 单例
    private LoginImService(){};
    private static class holder{
        private static LoginImService instance = new LoginImService();
    }
    public static LoginImService getInstance()
    {
        return  holder.instance;
    }

    // TLS登录回调接口
    private static PwdLoginListener pwdLoginListener;


    /**
     * 是否已有用户登录
     */
    public boolean isUserLogin() {
        return (!Kits.Empty.check(ImUser.getInstance().getImAccount())) && (!TLSService.getInstance().needLogin(ImUser.getInstance().getImAccount()));

    }

    // TLS登录
    public void loginTls(String username,String password) {
        if (Kits.Empty.check(username))
        {
            XLog.d(TAG, "loginTls - ImUser's ImAccount is null ");
            return;
        }

        if(Kits.Empty.check(pwdLoginListener)){
            pwdLoginListener = new PwdLoginListener();
        }
        TLSService.getInstance().TLSPwdLogin(username, password, pwdLoginListener);
    }

    // IM登录
    public void loginIm()
    {
        if (Kits.Empty.check(ImUser.getInstance().getImAccount()))
        {
            XLog.d(TAG, "loginIm - ImUser's ImAccount is null ");
            return;
        }
        // 登录腾讯IM
        LoginBusiness.loginIm(ImUser.getInstance().getImAccount(), ImUser.getInstance().getImSign(), this);
    }

    /**
     * imsdk登录失败后回调
     */
    @Override
    public void onError(int i, String s) {
        XLog.e(TAG, "IM登录错误回调  code " + i + " " + s);
        switch (i) {
            case 6208:
                XLog.e(TAG, "IM登录错误回调 6208 " + ResourceUtil.getResources().getString(R.string.kick_logout));
                // 离线状态下被其他终端踢下线

                break;
            case 6200:
                // 登录失败，当前无网络
                XLog.e(TAG, "IM登录错误回调 6200 " + ResourceUtil.getResources().getString(R.string.login_error_timeout));
                break;
            default:
                // 登录失败，请稍后重试
                XLog.e(TAG, "IM登录错误回调 default " + ResourceUtil.getResources().getString(R.string.login_error_timeout));
                break;
        }
    }

    /**
     * imsdk登录成功后回调
     */
    @Override
    public void onSuccess() {

        XLog.d(TAG, "IM登录登录成功 当前环境 " + TIMManager.getInstance().getEnv());
        //初始化程序后台后消息推送
        PushUtil.getInstance();
        //初始化消息监听
        MessageEvent.getInstance();
    }

    class PwdLoginListener implements TLSPwdLoginListener {
        @Override
        public void OnPwdLoginSuccess(TLSUserInfo userInfo) {
            XLog.d(TAG, "TLS后台登录成功");
            TLSService.getInstance().setLastErrno(0);
            String imAccount = TLSService.getInstance().getLastUserIdentifier();
            ImUser.getInstance().setImAccount(imAccount);
            ImUser.getInstance().setImSign(TLSService.getInstance().getUserSig(imAccount));
            loginIm();
        }

        @Override
        public void OnPwdLoginReaskImgcodeSuccess(byte[] picData) {

        }

        @Override
        public void OnPwdLoginNeedImgcode(byte[] picData, TLSErrInfo errInfo) {

        }

        @Override
        public void OnPwdLoginFail(TLSErrInfo errInfo) {
            TLSService.getInstance().setLastErrno(-1);
            XLog.e(TAG, "TLS后台登录失败" + errInfo);
        }

        @Override
        public void OnPwdLoginTimeout(TLSErrInfo errInfo) {
            TLSService.getInstance().setLastErrno(-1);
            XLog.e(TAG, "TLS后台登录失败" + errInfo);
        }
    }
}
